package com.ruoyi.web.controller.plan;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.ListProductDownLoad;
import com.ruoyi.system.service.IListProductDownLoadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 总成计划Controller
 *
 * @author ruoyi
 * @date 2022-11-21
 */
@RestController
@Api(tags = "总成计划信息")
@RequestMapping("/list/productDownLoad")
public class ListProductDownLoadController extends BaseController
{
    @Autowired
    private IListProductDownLoadService ListProductDownLoadService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询总成计划列表
     */
    @ApiOperation(value = "查询总成计划列表")
    @GetMapping("/list")
    public TableDataInfo list(@ApiParam(value = "总成计划参数", required = false) ListProductDownLoad listProductDownLoad)
    {
        startPage();
        List<ListProductDownLoad> list = ListProductDownLoadService.selectListProductDownLoadList(listProductDownLoad);
        return getDataTable(list);
    }

    /**
     * 生产队列批次调整
     */
    @ApiOperation(value = "根据生产线代码、批次号查询生产队列批次")
    @GetMapping("/searchList")
    public TableDataInfo searchListProductDownLoad(@ApiParam(value = "生产线代码", required = false) @PathVariable("lineCode") String lineCode,@ApiParam(value = "批次号", required = false) @PathVariable("batchNumber") String batchNumber)
    {
        startPage();
        List<ListProductDownLoad> list = ListProductDownLoadService.searchListProductDownLoad(lineCode,batchNumber);
        return getDataTable(list);
    }

    /**
     * 生产队列批次调整详情
     */
    @ApiOperation(value = "根据批次号查询批次单下车型")
    @GetMapping("/searchDetails")
    public TableDataInfo searchListProductDownLoadDetails(@ApiParam(value = "批次号", required = true) @PathVariable("batchNumber") String batchNumber)
    {
        startPage();
        List<ListProductDownLoad> list = ListProductDownLoadService.searchListProductDownLoadDetails(batchNumber);
        return getDataTable(list);
    }

    /**
     * 配单插入、产品插入
     */
    @ApiOperation(value = "根据产线号,获取在线队列")
    @GetMapping("/getList")
    public TableDataInfo getListProductDownload(@ApiParam(value = "产线号", required = true) @PathVariable("productLineCode") String productLineCode)
    {
        startPage();
        List<ListProductDownLoad> list = ListProductDownLoadService.getListProductDownload(productLineCode);
        return getDataTable(list);
    }

    /**
     * 查询总成计划下拉接口
     */
    @ApiOperation(value = "查询总成计划下拉接口")
    @GetMapping("/interface")
    public AjaxResult interfaces(@ApiParam(value = "总成计划参数", required = false) ListProductDownLoad listProductDownLoad)
    {
        List<ListProductDownLoad> list = ListProductDownLoadService.selectListProductDownLoadList(listProductDownLoad);
        return AjaxResult.success(list);
    }

    /**
     * 导出总成计划列表
     */
    @Log(title = "总成计划", businessType = BusinessType.EXPORT)
    @ApiOperation(value = "导出总成计划列表")
    @GetMapping("/export")
    public AjaxResult export(HttpServletResponse response, @ApiParam(value = "总成计划参数", required = false) ListProductDownLoad listProductDownLoad)
    {
        List<ListProductDownLoad> list = ListProductDownLoadService.selectListProductDownLoadList(listProductDownLoad);
        ExcelUtil<ListProductDownLoad> util = new ExcelUtil<ListProductDownLoad>(ListProductDownLoad.class);
        return util.exportExcel( list, "总成计划数据");
    }

    /**
     * 导入总成计划列表
     */
    @Log(title = "总成计划", businessType = BusinessType.IMPORT)
    @ApiOperation(value = "导入总成计划列表")
    @PostMapping("/importData")
    public AjaxResult importData(@ApiParam(value = "导入文件", required = true) MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<ListProductDownLoad> util = new ExcelUtil<ListProductDownLoad>(ListProductDownLoad.class);
        List<ListProductDownLoad> listProductDownLoadList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = ListProductDownLoadService.importListProductDownLoad(listProductDownLoadList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 导入模板
     */
    @ApiOperation(value = "导入模板")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        List<ListProductDownLoad> list = ListProductDownLoadService.selectListProductDownLoadList(new ListProductDownLoad());
        ExcelUtil<ListProductDownLoad> util = new ExcelUtil<ListProductDownLoad>(ListProductDownLoad.class);
        return util.importTemplateExcel(list,"总成计划数据");
    }

    /**
     * 获取总成计划详细信息
     */
    @ApiOperation(value = "获取总成计划详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@ApiParam(value = "总成计划id", required = true) @PathVariable("id") Long id)
    {
        return AjaxResult.success(ListProductDownLoadService.selectListProductDownLoadById(id));
    }

    /**
     * 新增总成计划
     */
    @Log(title = "总成计划", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增总成计划")
    @PostMapping
    public AjaxResult add(@ApiParam(value = "总成计划实体类", required = true) @RequestBody ListProductDownLoad listProductDownLoad)
    {

        return toAjax(ListProductDownLoadService.insertListProductDownLoad(listProductDownLoad));
    }

    /**
     * 修改总成计划
     */
    @Log(title = "总成计划", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改总成计划")
    @PutMapping
    public AjaxResult edit(@ApiParam(value = "总成计划实体类", required = true) @RequestBody ListProductDownLoad listProductDownLoad)
    {

        return toAjax(ListProductDownLoadService.updateListProductDownLoad(listProductDownLoad));
    }

    /**
     * 删除总成计划
     */
    @Log(title = "总成计划", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除总成计划")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@ApiParam(value = "总成计划id数组", required = true) @PathVariable Long[] ids)
    {
        return toAjax(ListProductDownLoadService.deleteListProductDownLoadByIds(ids));
    }
}
